package com.atguigu.gmall.juc.atomic;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author 陈昆昆
 * @version 1.0
 * @date 2020/6/2 15:27
 */
public class DoWhile {
    private static AtomicReference<Thread> atomicReference = new AtomicReference<>();

    public static void lock(){
        Thread thread = Thread.currentThread();
        while (!atomicReference.compareAndSet(null,thread)){

        }
        System.out.println(thread.getName()+"come in");
    }
    public static void unlock(){
        Thread thread = Thread.currentThread();
        atomicReference.compareAndSet(thread,null);
        System.out.println(thread.getName()+"unlock");
    }

    public static void main(String[] args) {
        new Thread(()->{
            DoWhile.lock();
            try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace();}
            DoWhile.unlock();
        },"AA").start();

        new Thread(()->{
            DoWhile.lock();
            try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace();}
            DoWhile.unlock();
        },"BB").start();
    }
}
